package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.CityDirectoryMapper;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.CityDirectory;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.RegionPageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.RegionUpsertReqDTO;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.*;
import com.jzo2o.foundations.service.IConfigRegionService;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 区域服务管理
 *
 * @author itcast
 * @create 2023/7/17 16:50
 **/
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Autowired
    private ServeItemMapper serveItemMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Override
    public PageResult<ServeResDTO> findByPage(ServePageQueryReqDTO servePageQueryReqDTO) {
        return PageHelperUtils.selectPage(servePageQueryReqDTO, () -> baseMapper.queryListByRegionId(servePageQueryReqDTO.getRegionId()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(List<ServeUpsertReqDTO> dtoList) {
        for (ServeUpsertReqDTO dto : dtoList) {
            ServeItem serveItem = serveItemMapper.selectById(dto.getServeItemId());
            if (ObjectUtil.isNull(serveItem)||serveItem.getActiveStatus()!=2) {
                throw new ForbiddenOperationException("服务项目状态有误");
            }
            Integer count = this.lambdaQuery().eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();
            if (count>0) {
                throw new ForbiddenOperationException("该区域已添加过该服务");
            }
            Serve serve = BeanUtil.copyProperties(dto,Serve.class);
            Region region = regionMapper.selectById(dto.getRegionId());
            if (ObjectUtil.isNotNull(region)){
                serve.setCityCode(region.getCityCode());
            }
            baseMapper.insert(serve);
        }
    }

    @Override
    public void deleteById(Long id) {
        Serve serve = baseMapper.selectById(id);
        if (ObjectUtil.isNull(serve)||serve.getSaleStatus()!=0) {
            throw new ForbiddenOperationException("服务不存在");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public void onSale(Long id) {
        Serve serve = baseMapper.selectById(id);
        if (ObjectUtil.isNull(serve)||serve.getSaleStatus()==2) {
            throw new ForbiddenOperationException("服务已上架");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (ObjectUtil.isNull(serveItem)||serveItem.getActiveStatus()!=2) {
            throw new ForbiddenOperationException("服务状态有误");
        }
        serve.setSaleStatus(2);
        baseMapper.updateById(serve);
    }

    @Override
    public void offSale(Long id) {
        Serve serve = baseMapper.selectById(id);
        if (ObjectUtil.isNull(serve)||serve.getSaleStatus()!=2) {
            throw new ForbiddenOperationException("服务状态异常");
        }
        serve.setSaleStatus(1);
        baseMapper.updateById(serve);
    }

    @Override
    public List<ServeCategoryResDTO> queryActiveRegionListCache(Long regionId) {
        Region region = regionMapper.selectById(regionId);
        if (ObjectUtil.isNull(region)||region.getActiveStatus()!=2) {
            return Collections.emptyList();
        }
        List<ServeCategoryResDTO> list = baseMapper.findListByRegionId(regionId);
        if (CollUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        list = CollUtil.sub(list,0,Math.min(list.size(),2));
        list.forEach(e -> e.setServeResDTOList(CollUtil.sub(e.getServeResDTOList(),0,Math.min(e.getServeResDTOList().size(),4))));
        return list;
    }

    @Override
    public List<ServeAggregationSimpleResDTO> hotServeList(Long regionId) {
        Region region = regionMapper.selectById(regionId);
        if (ObjectUtil.isNull(region)||region.getActiveStatus()!=2) {
            return Collections.emptyList();
        }
        return  baseMapper.findServeListByRegionId(regionId);
    }

    @Override
    public List<ServeAggregationTypeSimpleResDTO> serveTypeList(Long regionId) {
        //1 对区域进行校验
        Region region = regionMapper.selectById(regionId);
        if (ObjectUtil.isNull(region) || region.getActiveStatus() != 2) {
            return Collections.emptyList();
        }

        //2 查询当前区域下上架服务对应的分类
        return baseMapper.findServeTypeListByRegionId(regionId);
    }


}
